package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.*;
import com.baomidou.ant.mapper.ReOrderMapper;
import com.baomidou.ant.service.IPinLibraryService;
import com.baomidou.ant.service.IReOrderService;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.service.IWorkOrderService;
import com.baomidou.ant.utils.TemplateExcelUtils;
import com.baomidou.ant.vo.request.RequestAddReProVo;
import com.baomidou.ant.vo.request.RequestListReOrderVo;
import com.baomidou.ant.vo.response.ResponseListReOrderVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@Service
public class ReOrderServiceImpl extends ServiceImpl<ReOrderMapper, ReOrder> implements IReOrderService {
    @Autowired
    IWorkOrderService workOrderService;
    @Autowired
    ISysUserService userService;
    @Value("${file.reProFilePath}")
    private String reProFilePath;
    @Autowired
    IPinLibraryService pinLibraryService;

    @Override
    public String addReOrder(RequestAddReProVo vo) {
        QueryWrapper<ReOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("old_lot",vo.getOldLot());
        //得到所有返工单
        List<ReOrder> allList=list(queryWrapper);
        queryWrapper.eq("status","0");
        //得到未审核返工单
        List<ReOrder> notCheckList=list(queryWrapper);
        if(notCheckList!=null&&notCheckList.size()>0){
            //存在未审核
            return "EXIST_NOT_CHECK";
        }else {
            ReOrder reOrder=new ReOrder();
            WorkOrder workOrder=workOrderService.queryByOrderNum(vo.getOldLot());
            if(ObjectUtils.isEmpty(workOrder)){
                return "OLD_LOT_ERROR";
            }
            if(!workOrder.isCheck()){
                return "NOT_START";
            }
            reOrder.setOldLot(vo.getOldLot());
            //创建人写入
            SysUser user=userService.queryByLoginName(vo.getCreater());
            if(ObjectUtils.isEmpty(user)){
                reOrder.setCreater("未知");
            }else
                reOrder.setCreater(user.getLoginName()+" "+user.getUserName());
            reOrder.setPinId(workOrder.getPinId());
            reOrder.setDeptId(workOrder.getOrderDept());
            //编号
            String relot=vo.getOldLot().replace('J','K');
            int num=allList.size()+1;
            relot=relot+"-"+num;
            reOrder.setReLot(relot);
            if(save(reOrder)){
                return "SUCCESS";
            }else
                return "FAIL";
        }
    }

    @Override
    public IPage<ResponseListReOrderVo> reOrderList(RequestListReOrderVo vo) {
        IPage<ReOrder> iPage=new Page<>(vo.getDisplayStart(),vo.getDisplayLength());
        QueryWrapper<ReOrder> queryWrapper=new QueryWrapper<>();
        //原工单号查询
        if(StringUtils.isNotBlank(vo.getOldLot())){
            queryWrapper.like("old_lot",vo.getOldLot());
        }
        //返工单号查询
        if(StringUtils.isNotBlank(vo.getReLot())){
            queryWrapper.like("re_lot",vo.getReLot());
        }
        //部门查询
        SysUser user=userService.getById(vo.getUserId());
        //判断管理员级别
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(ObjectUtils.isNotEmpty(vo.getOrderDept())){
                queryWrapper.eq("dept_id",vo.getOrderDept());
            }
        } else if(Objects.equals(user.getType(), "0")||Objects.equals(user.getType(), "2")){//只能看自己部门的
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        //返工单号查询
        if(StringUtils.isNotBlank(vo.getStatus())){
            queryWrapper.eq("status",vo.getStatus());
        }
        IPage<ReOrder> page=page(iPage,queryWrapper);
        return page.convert(reOrder -> {
            ResponseListReOrderVo responseListReOrderVo=new ResponseListReOrderVo();
            BeanUtils.copyProperties(reOrder,responseListReOrderVo);
            return responseListReOrderVo;
        });
    }

    @Override
    public ReOrder queryByReLot(String reLot) {
        QueryWrapper<ReOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("re_lot",reLot);
        List<ReOrder> reOrderList=list(queryWrapper);
        if(reOrderList!=null&&reOrderList.size()>0){
            return reOrderList.get(0);
        }else
            return null;
    }

    @Override
    public void exportTemplateProvinceAreaData(HttpServletResponse response, String reLot) {
        try {
            ReOrder reOrder=queryByReLot(reLot);
            //得到工单的产品信息
            String fileName=reOrder.getReLot();
//            PinLibrary pinLibrary=pinLibraryService.getById(parentOrder.getPinId());
            PinLibrary pinLibrary=pinLibraryService.getById(reOrder.getPinId());
            //数据填入
            Map<String,Object> param = new HashMap<>();
            param.put("oldLot",reOrder.getOldLot());
            param.put("reLot",reOrder.getReLot());
            param.put("pinName",pinLibrary.getPinName());
            //文件路径
            //pinLibrary.getPinName()+".xlsx"
            TemplateExcelUtils.downLoadExcel(fileName,reProFilePath,param,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
